<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-10-18
 * Time: 下午6:36
 * 完善用户个人信息
 */
namespace Api\Home;

use Common\HttpStatus;
use Common\PlutoApi;
use Common\Request;
use Common\RequestApi;
use Common\Response;
use Common\ResponseApi;
use DB\Home\AccountUser;
use DB\Home\AccountUserInfo;

class PerfectInfoRequest extends RequestApi{
  public $name;
  public $phone;
  public $nickname;
}

class PerfectInfoResponse extends ResponseApi{
  public $message;
}

class PerfectInfo extends PlutoApi{
  protected function run(Request $request, Response $response) {
    /**
     * @var $rqData PerfectInfoRequest
     */
    $rqData = $request->data;
    if (empty($rqData->name) || !is_string($rqData->name)){
      $response->httpStatus = HttpStatus::PARAMETER_ERROR;
      $response->httpMessage = "name is not set or not string";
      return;
    }
    if (empty($rqData->nickname) || !is_string($rqData->nickname)){
      $response->httpStatus = HttpStatus::PARAMETER_ERROR;
      $response->httpMessage = "nickname is not set or is not string";
      return;
    }
    if (empty($rqData->phone) || !is_numeric($rqData->phone)){
      $response->httpStatus = HttpStatus::PARAMETER_ERROR;
      $response->httpMessage = "phone is not set or is not numeric";
      return;
    }
    $user = new AccountUser($request->uid);
    $info = new AccountUserInfo();
    $user->getInfo($info);
    $info->name = $rqData->name;
    $info->nickname = $rqData->nickname;
    $info->phone = $rqData->phone;
    if(!$user->update($info)){
      $response->httpStatus = HttpStatus::ERROR;
      $response->httpMessage = "update user info failed";
      return;
    }
    $response->data = new PerfectInfoResponse();
    $response->data->message = "更新成功";
  }
}